import { IRootState } from '@/store/type'
import { Module } from 'vuex'
import { ISystemState } from './type'

import {
  deletePageData,
  getPageListData ,
  createPageData,
  editPageData
} from '@/service/main/system/system'
import user from '@/router/main/system/user/user'

const systemModule : Module<ISystemState,IRootState> = {
  namespaced:true,
  state(){
    return {
      usersList:[],
      usersCount:0,
      roleList:[],
      roleCount:0,
      goodsList:[],
      goodsCount:0,
      menuList:[],
      menuCount:0
    }
  },
  mutations:{
    changeUsersList(state, userList: any[]){
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number){
      state.usersCount = userCount
    },
    changeRoleList(state, list: any[]){
      state.roleList = list
    },
    changeRoleCount(state, count: number){
      state.roleCount = count
    },
    changeGoodsList(state, list: any[]){
      state.goodsList = list
    },
    changeGoodsCount(state, count: number){
      state.goodsCount = count
    },
    changeMenuList(state, list: any[]){
      state.menuList = list
    },
    changeMenuCount(state, count: number){
      state.menuCount = count
    }
  },
  getters:{
    pageListData(state){
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
        // switch (pageName) {
        //   case 'users':
        //     return state.usersList
        //   case 'role':
        //     return state.roleList
        // }
      }
    },
    pageListCount(state){
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions:{
    //获取列表数据
    async getPageListAction({commit},payload: any){
      console.log(payload)
      //获取pageUrl
      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`
      // switch (pageName){
      //   case "users":
      //     pageUrl = '/users/list'
      //     break;
      //   case "role":
      //     pageUrl = "/role/list"
      //     break;
      // }
      //1.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo);
      console.log(pageResult)
      //拿到数据和总数
      const { list , totalCount } = pageResult.data;
      commit(`change${pageName.replace(pageName[0],pageName[0].toUpperCase())}List`, list);
      commit(`change${pageName.replace(pageName[0],pageName[0].toUpperCase())}Count`, totalCount)
    },
    //删除用户列表
    async deletePageDataAction({dispatch},payload:any){
      //1.pageName
      //2.id
      const {pageName,id} = payload;
      const pageUrl = `/${pageName}/${id}`
      //调用删除网络请求
      await deletePageData(pageUrl)
      //重新请求最新数据
      dispatch('getPageListAction',{
        pageName:pageName,
        queryInfo:{
          offset:0,
          size:10
        }
      })
    },
    //新建用户
    async createPageDataAction({dispatch},payload: any){
      //创建请求
      const {pageName,newData} = payload;
      const pageUrl = `/${pageName}`;
      await createPageData(pageUrl,newData)
      //请求最新数据
      dispatch('getPageListAction',{
        pageName:pageName,
        queryInfo:{
          offset:0,
          size:10
        }
      })
    },
    //编辑用户
    async editPageDataAction({dispatch},payload: any){
       //创建请求
       const {pageName,editData,id} = payload;
       const pageUrl = `/${pageName}/${id}`;
       await editPageData(pageUrl,editData)
      //请求最新数据
      dispatch('getPageListAction',{
        pageName:pageName,
        queryInfo:{
          offset:0,
          size:10
        }
      })
    }
  }
}

export default systemModule
